package com.ccp.dev.system.controller;
import com.ccp.dev.core.basic.annotion.Action;
import com.ccp.dev.core.basic.consts.UrlConstants;
import com.ccp.dev.core.basic.internationalization.SysBaseController;
import com.ccp.dev.core.basic.model.SysAuditModelType;
import com.ccp.dev.core.basic.response.ResultData;
import com.ccp.dev.core.basic.util.ContextUtil;
import com.ccp.dev.core.basic.util.UUIDUtils;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.core.util.BeanUtils;
import com.ccp.dev.core.util.RequestUtil;
import com.ccp.dev.core.util.log.SysAuditThreadLocalHolder;
import com.ccp.dev.system.model.MessageReply;
import com.ccp.dev.system.model.MessageSend;
import com.ccp.dev.system.model.SysUser;
import com.ccp.dev.system.service.MessageReadService;
import com.ccp.dev.system.service.MessageReplyService;
import com.ccp.dev.system.service.MessageSendService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 对象功能:消息回复 控制器类
 * 开发人员:csx
 * 创建时间:2012-01-14 15:15:43
 */
@Controller
@Action(ownermodel= SysAuditModelType.USER_MANAGEMENT)
@RequestMapping(UrlConstants.PROJECT_ROOT + UrlConstants.PROJECT_MODULE_SYSTEM + "/messageReply")
public class MessageReplyController extends SysBaseController {
    @Resource
    private MessageReplyService replyService;
    @Resource
    private MessageSendService sendService;
    @Resource
    private MessageReadService readService;
    private static final String SUCCESS = "operation.success";
    private static final String FAILURE = "operation.failure";



    /**
     * 取得消息回复分页列表
     * @param request
     * @return ResultData
     */
    @RequestMapping(UrlConstants.MODULE_FUNCTION_LIST)
    @Action(description="消息回复分页列表",detail="查询消息回复分页列表<#if success>成功<#else>失败</#if>",exectype = "查询日志")
    @ResponseBody
    public ResultData list(HttpServletRequest request) {
        try {
            SysUser curUser = (SysUser) ContextUtil.getCurrentUser();
            String userId = curUser.getUserId();
            Boolean isUser = RequestUtil.getBoolean(request,"isUser");
            String messageId = RequestUtil.getString(request, "messageId");
            MessageSend messageSend = sendService.getById(messageId);
            QueryFilter queryFilter = new QueryFilter(request);
            queryFilter.addFilter("userId", userId);
            List<MessageReply> list = new ArrayList<>();
            if(isUser){
                list = replyService.getAllByUserName(queryFilter);
            }else{
                list = replyService.getAll(queryFilter);
            }
            SysAuditThreadLocalHolder.putParamerter("success", true);
            return getResultDataSuccess(SUCCESS, queryFilter.getPageBean().getTotalCount(), list);
        } catch (Exception e) {
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataError(FAILURE);
        }
    }
    /**
     * 取得消息回复页面信息
     * @param request
     * @param
     * @return
     */
    @RequestMapping("/edit")
    @Action(description="消息回复页面",detail="消息回复页面获取数据<#if success>成功<#else>失败</#if>",exectype = "查询日志")
    @ResponseBody
    public ResultData edit(HttpServletRequest request) {
        try {
            String id = RequestUtil.getString(request, "id");
            String messageId = RequestUtil.getString(request, "messageId");
            SysUser curUser = (SysUser) ContextUtil.getCurrentUser();
            String userId = curUser.getUserId();
            MessageReply messageReply = null;
            MessageSend messageSend = null;
            messageSend = sendService.getById(messageId);
            if (BeanUtils.isNotEmpty(id)) {
                messageReply = replyService.getById(id);
            } else {
                messageReply = new MessageReply();
                messageReply.setMessageId(messageId);
                messageReply.setReply(curUser.getFullName());
                messageReply.setReplyId(userId);
            }
            SysAuditThreadLocalHolder.putParamerter("success", true);
            return getResultDataSuccess(SUCCESS, 0, messageSend);
        } catch (Exception e) {
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataError(FAILURE);
        }
    }

    /**
     * 取得消息回复明细
     * @param request
     * @return
     */
    @RequestMapping(UrlConstants.MODULE_FUNCTION_GET)
    @Action(description="消息回复明细",detail="查询消息回复明细<#if success>成功<#else>失败</#if>",exectype = "查询日志")
    @ResponseBody
    public ResultData get(HttpServletRequest request) {
        try{
            String id = RequestUtil.getString(request, "id");
            MessageReply messageReply = replyService.getById(id);
            return getResultDataSuccess(SUCCESS, messageReply);
        }catch (Exception e){
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataError(FAILURE);
        }
    }

  /**
     * 添加或更新消息回复
     * @param request
     * @param messageReply 添加或更新的实体
     * @return ResultData
     * @throws Exception
     */
    @RequestMapping(UrlConstants.MODULE_FUNCTION_ADD_UPDATE)
    @Action(description="添加或更新消息回复",
            detail="添加或更新消息回复消息<#if success>成功<#else>失败</#if>",
            exectype = "编辑日志")
    @ResponseBody
    public ResultData save(@RequestBody MessageReply messageReply, HttpServletRequest request) throws Exception {
       try{
            String messageId = RequestUtil.getString(request, "messageId");
            String replyId = ContextUtil.getCurrentUser().getUserId();
            Date now = new Date();
            SysUser curUser = (SysUser) ContextUtil.getCurrentUser();
            messageReply.setId(UUIDUtils.getUUIDFor32());
            messageReply.setReplyTime(now);
            messageReply.setReplyId(replyId);
            messageReply.setReply(curUser.getFullName());
            //TODO 数据库事务问题
            replyService.add(messageReply);
            readService.addMessageRead(messageReply.getMessageId(), curUser);
           SysAuditThreadLocalHolder.putParamerter("success", true);
           return getResultDataSuccess(SUCCESS);
       }catch (Exception e){
           SysAuditThreadLocalHolder.putParamerter("success", false);
           return getResultDataError(FAILURE);
       }
    }

    /*                                              页面跳转
     *************************************************************************************************************/

    /**
     * 回复消息页面跳转
     * @param request
     * @param model
     * @return ResultData
     * @throws Exception
     */
    @GetMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/edit")
    @Action(description = "回复消息页面页面跳转", exectype = "页面跳转日志")
    public String viewRunningList(HttpServletRequest request, Model model) {
        String id = RequestUtil.getString(request, "id");
        String messageId = RequestUtil.getString(request, "messageId");
        model.addAttribute("id", id);
        model.addAttribute("messageId", messageId);
        return "messageReply/edit";
    }
}
